package com.sgcc.operate.service.impl;

import java.text.ParseException;
import java.util.*;

import com.sgcc.common.core.utils.DateUtils;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.operate.domain.OActiInfo;
import com.sgcc.operate.mapper.OActiInfoMapper;
import org.activiti.engine.impl.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.operate.mapper.OActiStatMapper;
import com.sgcc.operate.domain.OActiStat;
import com.sgcc.operate.service.IOActiStatService;

/**
 * 活动趋势统计Service业务层处理
 *
 * @author sgcc
 * @date 2021-03-31
 */
@Service
public class OActiStatServiceImpl implements IOActiStatService {
    @Autowired
    private OActiStatMapper oActiStatMapper;
    @Autowired
    private OActiInfoMapper oActiInfoMapper;

    /**
     * 查询活动趋势统计
     *
     * @param statDate 活动趋势统计ID
     * @return 活动趋势统计
     */
    @Override
    public OActiStat selectOActiStatById(String statDate) {
        return oActiStatMapper.selectOActiStatById(statDate);
    }

    /**
     * 查询活动趋势统计列表
     *
     * @param oActiStat 活动趋势统计
     * @return 活动趋势统计
     */
    @Override
    public List<OActiStat> selectOActiStatList(OActiStat oActiStat) {
        return oActiStatMapper.selectOActiStatList(oActiStat);
    }

    /**
     * 新增活动趋势统计
     *
     * @param oActiStat 活动趋势统计
     * @return 结果
     */
    @Override
    public int insertOActiStat(OActiStat oActiStat) {
        return oActiStatMapper.insertOActiStat(oActiStat);
    }

    /**
     * 修改活动趋势统计
     *
     * @param oActiStat 活动趋势统计
     * @return 结果
     */
    @Override
    public int updateOActiStat(OActiStat oActiStat) {
        return oActiStatMapper.updateOActiStat(oActiStat);
    }

    /**
     * 批量删除活动趋势统计
     *
     * @param statDates 需要删除的活动趋势统计ID
     * @return 结果
     */
    @Override
    public int deleteOActiStatByIds(String[] statDates) {
        return oActiStatMapper.deleteOActiStatByIds(statDates);
    }

    /**
     * 删除活动趋势统计信息
     *
     * @param statDate 活动趋势统计ID
     * @return 结果
     */
    @Override
    public int deleteOActiStatById(String statDate) {
        return oActiStatMapper.deleteOActiStatById(statDate);
    }


    @Override
    public AjaxResult middleStatistic(String actiId, String type, String dateRange) {
        AjaxResult ajaxResult = AjaxResult.success();
        List<OActiStat> data = null;
        Date now = new Date();
        if ("01".equals(type) || "02".equals(type)) {
            data = oActiStatMapper.statistic1(type, actiId);
            String[] dateStrs = {"00", "01", "02", "03", "04", "05", "06", "07",
                    "08", "09", "10", "11", "12", "13", "14", "15",
                    "16", "17", "18", "19", "20", "21", "22", "23"};
            ajaxResult.put("dateStrs", dateStrs);
            arrangeData(data, dateStrs, ajaxResult);
        } else if ("03".equals(type)) {
            Date begin = DateUtils.addDays(now, -8);
            Date end = DateUtils.addDays(now, -1);
            String[] dateStrs = getDateArray(begin, end);
            ajaxResult.put("dateStrs", dateStrs);
            data = oActiStatMapper.statistic2(type, actiId, null, null);
            arrangeData(data, dateStrs, ajaxResult);
        } else if ("04".equals(type)) {
            Date begin = DateUtils.getMonthStart();
            Date end = DateUtils.getMonthEnd();
            String[] dateStrs = getDateArray(begin, end);
            ajaxResult.put("dateStrs", dateStrs);
            data = oActiStatMapper.statistic2(type, actiId, null, null);
            arrangeData(data, dateStrs, ajaxResult);
        } else if ("05".equals(type)) {
            OActiInfo actiInfo = oActiInfoMapper.selectOActiInfoById(actiId);
            try {
                Date begin = DateUtils.parseDate(DateUtils.formatDate(actiInfo.getStarttime(), "yyyy-MM-dd"), "yyyy-MM-dd");
                Date end = DateUtils.parseDate(DateUtils.formatDate(actiInfo.getEndtime(), "yyyy-MM-dd"), "yyyy-MM-dd");
                String[] dateStrs = getDateArray(begin, end);
                ajaxResult.put("dateStrs", dateStrs);
                data = oActiStatMapper.statistic2(type, actiId, actiInfo.getStarttime(), actiInfo.getEndtime());
                arrangeData(data, dateStrs, ajaxResult);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if ("06".equals(type)) {
            if (StringUtils.isEmpty(dateRange)) {
                return AjaxResult.error("时间范围不能为空！");
            }
            String[] dates = dateRange.split("~");
            try {
                Date begin = DateUtils.parseDate(dates[0].trim(), "yyyy-MM-dd");
                Date end = DateUtils.parseDate(dates[1].trim(), "yyyy-MM-dd");
                String[] dateStrs = getDateArray(begin, end);
                ajaxResult.put("dateStrs", dateStrs);
                data = oActiStatMapper.statistic2(
                        type, actiId,
                        begin,
                        end
                );
                arrangeData(data, dateStrs, ajaxResult);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return ajaxResult;
    }


    @Override
    public AjaxResult leftDownStatistic(String actiId) {
        return AjaxResult.success(oActiStatMapper.statistic3(actiId));
    }

    @Override
    public AjaxResult rightDownStatistic(String actiId, String areaNo) {
        if (StringUtils.isEmpty(actiId)) {
            return AjaxResult.error("活动id不能为空！");
        }
        if (StringUtils.isEmpty(areaNo)) {
            List<Map<String, Object>> ranks = oActiStatMapper.statistic3(actiId);
            if (!CollectionUtil.isEmpty(ranks)) {
                areaNo = ranks.get(0).get("areaNo").toString();
            } else {
                areaNo = "350100";
            }
        }
        List<Map<String, Object>> result = oActiStatMapper.statistic4(actiId, areaNo);
        List<Integer> nparts = new ArrayList<Integer>();
        List<String> areaNames = new ArrayList<String>();
        for (Map<String, Object> map : result) {
            Double npart = Double.parseDouble(map.get("npart").toString());
            nparts.add(npart.intValue());

            areaNames.add(map.get("areaName").toString());
        }
        Map map = new HashMap();
        map.put("nparts", nparts);
        map.put("areaNames", areaNames);
        return AjaxResult.success(map);
    }

    private String[] getDateArray(Date begin, Date end) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(begin);
        List<String> dateStrs = new ArrayList<String>();
        while (!calendar.getTime().equals(end)) {
            dateStrs.add(DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd"));
            calendar.add(Calendar.DATE, 1);
        }
        dateStrs.add(DateUtils.formatDate(end, "yyyy-MM-dd"));
        return dateStrs.toArray(new String[dateStrs.size() - 1]);
    }

    private AjaxResult arrangeData(List<OActiStat> result, String[] dateStrs, AjaxResult j) {
        List<Integer> nparts = new ArrayList<Integer>();
        List<Integer> nlogs = new ArrayList<Integer>();
        List<Integer> nshares = new ArrayList<Integer>();
        List<Integer> nprizes = new ArrayList<Integer>();
        for (String dateStr : dateStrs) {
            boolean exist = false;
            for (OActiStat stat : result) {
                if (dateStr.equals(stat.getTimeRange())) {
                    nparts.add(stat.getnPart().intValue());
                    nlogs.add(stat.getnLog().intValue());
                    nshares.add(stat.getnShare().intValue());
                    nprizes.add(stat.getnPrize().intValue());
                    exist = true;
                    break;
                }
            }
            if (!exist) {
                nparts.add(0);
                nlogs.add(0);
                nshares.add(0);
                nprizes.add(0);
            }
        }
        j.put("nparts", nparts);
        j.put("nlogs", nlogs);
        j.put("nshares", nshares);
        j.put("nprizes", nprizes);
        return j;
    }
}
